<?php

defined('SYSPATH') or die('No direct script access.');
// test
/**
 * domyslny contoller, wymaga zalogowania
 */
class Controller_main extends Controller_Template {

    protected $scripts = array();
    public $template = 'main/template';
    
    private $_topActionMenu = array();

    /**
     * The before() method is called before your controller action.
     * In our template controller we override this method so that we can
     * set up default values. These variables are then available to our
     * controllers if they need to be modified.
     */
    public function before() {
        parent::before();

        /////////////
        $auth = Auth::instance();
        $logged_in = $auth->logged_in('login');

        $user_model = Model::factory('user', $auth->get_user());
        $this->user_data = $user_model->as_array();
        
        if (!$logged_in) {
            $this->request->redirect('user/login');
        }
        
        if($_REQUEST['window']){
            $this->template->window = true;
        }

        if ($this->auto_render) {
            // Initialize empty values
            $this->template->title = 'SZF';
            $this->template->content = '';

            $this->template->styles = array();
            $this->template->scripts = array();
        }
    }

    /**
     * The after() method is called after your controller action.
     * In our template controller we override this method so that we can
     * make any last minute modifications to the template before anything
     * is rendered.
     */
    public function after() {

        Model_Settings::init();

        if ($this->auto_render) {
            $styles = array(
                'media/css/main.css' => 'screen',
                'media/css/sprite.css' => 'screen',
                'media/js/calendar/skins/default/calendar.css' => 'screen',
            );

            array_unshift($this->scripts, 'media/js/main.js');
            array_unshift($this->scripts, 'media/js/mootools-more-1.3.2.1.js');
            array_unshift($this->scripts, 'media/js/mootools-core-1.3.2-full-compat-yc.js');

            $this->scripts[] = 'media/js/calendar/calendar.js';
            $this->template->styles = $styles;
            $this->forceScriptsRefresh();
            
            $this->template->scripts = $this->scripts;

            $top_menu = Request::factory('static/topmenu')->execute()->body();
            $this->template->top_menu = $top_menu;
            $this->template->top_action_menu = $this->_topActionMenu();
//            $this->template->top_menu->user_data=$this->user_data;
        }
        if($_REQUEST['windowClear']){
            $this->template = $this->template->content;
        }
        parent::after();
    }
    
    protected function addTopActionMenu($text, $url, $color=null, $properties=array()){
        if($color){
            if($properties['class']){
                $properties['class'].=' top_action_menu_butt-'.$color;
            }else{
                $properties['class'].='top_action_menu_butt-'.$color;
            }
        }
        
        $this->_topActionMenu[] = array(
            'text'=>$text,
            'url'=>$url,
            'properties'=>$properties,
        );
    }

    private function _topActionMenu(){
        if(!count($this->_topActionMenu)){
            return '';
        }
        
        $view = View::factory('main/top-action-menu');
        $view->menu = $this->_topActionMenu;
        
        $html = $view->render();
        return $html;
    }
    
    protected function forceScriptsRefresh(){
        $val = Kohana::$config->load('app')->javascriptRevision;
        foreach($this->scripts as &$v){
//            $val = rand(1, 1000);
            $v = $v."?$val";
        }
        unset($v);
    }

}